#include "driverController.h"
#include "view/camera/first_person_camera.h"


DriverController::DriverController(View* view,Model* model)
	:	SceneController(view,model){
	oldDir = dynamic_cast<SceneModel&>(*model).getTruck().getDirection();
}

DriverController::~DriverController(){

}

void DriverController::timer(int value){
	SceneController::timer(value);

	SceneModel& smodel = dynamic_cast<SceneModel&>(*model);

	SceneView& sview = dynamic_cast<SceneView&>(*view);
	FirstPersonCamera& camera = static_cast<FirstPersonCamera&>(sview.getCamera());

	Point center = smodel.getTruck().getDriverPosition();
	camera.moveTo(center);
	//camera.moveTo((center-camera.getEye())+camera.getEye());
	if(!(oldDir==smodel.getTruck().getDirection())){
		camera.rotateLR(smodel.getTruck().getZDiffRotated());
		oldDir = smodel.getTruck().getDirection();
	}
}

void DriverController::keyboard(unsigned char key,float x ,float y){
	SceneController::keyboard(key,x,y);

	//Para poder capturar los puntos del camino del camion
	SceneModel& smodel = dynamic_cast<SceneModel&>(*model);


	std::fstream out("path.data");
	if(!out.is_open())
		out.open("path.data",std::fstream::trunc);
	out.seekp(0,std::fstream::end);

	Point tdir = smodel.getTruck().getDirection();
	Point off = tdir*2.0f;
	float zrotdiff = smodel.getTruck().getZRotatedAngle();
	std::ostringstream strp;
	strp <<  smodel.getTruck().getDimensions().center().x1 << "," << smodel.getTruck().getDimensions().center().x2 << ",0.0\n";
	std::string unload = "UNLOAD_POINT\n";
	std::string load = "LOAD_POINT\n";

	switch(key){
		case 'i':
			smodel.getTruck().getDimensions().translate(off.x1,off.x2,0);
			break;
		case 'l':
			smodel.getTruck().setZRotatedAngle(smodel.getTruck().getZRotatedAngle()-0.1f);
			break;
		case 'j':
			smodel.getTruck().setZRotatedAngle(smodel.getTruck().getZRotatedAngle()+0.1f);
			break;
		case 'k':
			out.write(strp.str().c_str(),strp.str().size());
			break;
		case 'n':
			out.write(load.c_str(),load.size());
			break;
		case 'b':
			out.write(unload.c_str(),unload.size());
			break;
	}
	out.close();
	zrotdiff = smodel.getTruck().getZRotatedAngle()-zrotdiff;
	if(zrotdiff){
		Matrix4x4 m(zrotdiff,Point(0,0,1));
		smodel.getTruck().setDirection(m*smodel.getTruck().getDirection());
	}
}

void DriverController::redisplay(){
	SceneController::redisplay();

	SceneModel& smodel = dynamic_cast<SceneModel&>(*model);
	SceneView& sview = dynamic_cast<SceneView&>(*this->view);

	std::ostringstream stream;
	Truck& t = smodel.getTruck();
	Point pos = t.getDimensions().center();
	stream << "center : x: " << pos.x1 << " y: " << pos.x2 << " z: " << pos.x3 << std::endl;
	sview.renderText(stream.str(),0.65,0.70,GLUT_BITMAP_HELVETICA_18);

	std::ostringstream stream1;
	pos =  t.getIterator().getInitial();
	stream1 << "intial : x: " << pos.x1 << " y: " << pos.x2 << " z: " << pos.x3 << std::endl;
	sview.renderText(stream1.str(),0.65,0.65,GLUT_BITMAP_HELVETICA_18);

	std::ostringstream stream2;
	pos =  t.getIterator().getLast();
	stream2 << "last : x: " << pos.x1 << " y: " << pos.x2 << " z: " << pos.x3 << std::endl;
	sview.renderText(stream2.str(),0.65,0.60,GLUT_BITMAP_HELVETICA_18);

	std::ostringstream stream3;
	stream3 << "zang: " << t.getZRotatedAngle() << std::endl;
	sview.renderText(stream3.str(),0.65,0.55,GLUT_BITMAP_HELVETICA_18);

	std::ostringstream stream4;
	stream4 << "xang: " << t.getXRotatedAngle() << std::endl;
	sview.renderText(stream4.str(),0.65,0.50,GLUT_BITMAP_HELVETICA_18);
}
